<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-29 19:51:21 +0200 (Sun, 29 Jun 2008) $
 * $Rev: 1238 $
 * $Author: gavinspearhead $
 * $Id: admin_tasks.php 1238 2008-06-29 17:51:21Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);


$pathadt = realpath(dirname(__FILE__));

require_once "$pathadt/../functions/html_includes.php";
require_once "$pathadt/../functions/pref_functions.php";

if (!$isadmin)
	fatal_error($LN['error_noadmin']);

$sort = get_request('sort', 'command');
$sort_dir = strtolower(get_request('sort_dir', 'desc'));

if (!in_array($sort, array('command', 'at_time', 'interval', 'username')))
    fatal_error('invalid sortname');
if (!in_array($sort_dir, array('asc', 'desc')))
    fatal_error('invalid sortname');

$qry = "SELECT * FROM schedule ORDER BY \"$sort\" $sort_dir";
$res = $db->execute_query($qry);
$jobs = array();
if ($res === FALSE)
	$res = array();

foreach ($res as $row){
	$job['time'] = time_format($row['at_time']);
	$job['period'] = readable_time($row['interval']);
	$task = command_description($db, htmlentities($row['command']));
	$job['user'] = $row['username'];
	$task_long = '';
	if (strlen($task) > $prefs['maxsetname']) {
		$task_short = substr($task, 0, max(0, $prefs['maxsetname']-3) ) . '...';
		$task = $task_short;
		$task_long = $task;
	} else 
		$task_short = $task;
	$job['cmd'] = $row['command'];
	$job['task'] = $task_short;
	$job['task_popup'] = $task_long;
	$jobs[] = $job;
}


$urdd_online = check_urdd_online();
$challenge = set_challenge();
$smarty->assign('challenge',		$challenge);
$smarty->assign('alljobs',	$jobs);
$smarty->assign('order',	$sort);
$smarty->assign('order_dir',	$sort_dir);
$smarty->assign('urdd_online',    	(int)$urdd_online);
$smarty->display('ajax_adminjobs.tpl');

echo '&nbsp;';

?>
